<?php
class Db_Filter
{
	/**
	 * Hàm tạo, lấy và xóa cache với thuộc tính của Khách sạn, bao gồm nhóm thuộc tính và thuộc tính
	 *
	 * @param boolean $delcache: false: lấy cache; true: xóa cache
	 */
	
    static function getHotelFilters($delcache = false)
    {
		if(ArrCache::is_not_cached('hotel_filter_arr',0,'',$delcache))
		{
			if(!$delcache)
			{
				$re = DB::query('SELECT * FROM hotel_filter ORDER BY fgid, pos');
				
				if($re)
				{
					while ($filter = mysql_fetch_assoc($re))
					{
						if(!isset(CGlobal::$hotel_filters_bg[$filter['fgid']]))
						{
							CGlobal::$hotel_filters_bg[$filter['fgid']] = array();
						}
						
						CGlobal::$hotel_filters_bg[$filter['fgid']][$filter['id']]  = $filter;
						
						CGlobal::$hotel_filters[$filter['id']]  = $filter;
					}									
				}
				
				$re = DB::query('SELECT * FROM hotel_filter_group ORDER BY pos');
				
				if($re)
				{
					while ($group_filter = mysql_fetch_assoc($re))
					{
						CGlobal::$hotel_filter_groups[$group_filter['id']] = $group_filter;
						
						if(isset(CGlobal::$hotel_filters_bg[$group_filter['id']]))
						{
							$group_filter['filters'] = CGlobal::$hotel_filters_bg[$group_filter['id']];
						}
					}									
				}
				
				ArrCache::set(array(
									'hotel_filters'			=>CGlobal::$hotel_filters,
									'hotel_filters_bg'		=>CGlobal::$hotel_filters_bg,
									'hotel_filter_groups'	=>CGlobal::$hotel_filter_groups
									));
			}
		}
		else
		{
			CGlobal::$hotel_filters 		= ArrCache::$arr_cache['hotel_filters'];
			CGlobal::$hotel_filters_bg 		= ArrCache::$arr_cache['hotel_filters_bg'];
			CGlobal::$hotel_filter_groups 	= ArrCache::$arr_cache['hotel_filter_groups'];
			
			ArrCache::$arr_cache 	= array();
		}
	}
	
	/**
	 * Hàm tạo, lấy, xóa cache thuộc tính phòng Khách sạn
	 *
	 * @param boolean $delcache: false: lấy cache; true: xóa cache
	 */
	static function getRoomFilters($delcache = false)
    {
		if(ArrCache::is_not_cached('room_filters_arr',0,'',$delcache))
		{
			if(!$delcache)
			{
				$re = DB::query('SELECT * FROM room_filter ORDER BY pos');
				
				if($re)
				{
					while ($filter = mysql_fetch_assoc($re))
					{
						CGlobal::$room_filters[$filter['id']]  = $filter;
					}									
				}
				
				ArrCache::set(CGlobal::$room_filters);
			}
		}
		else
		{
			CGlobal::$room_filters 	= ArrCache::$arr_cache;
			
			ArrCache::$arr_cache 	= array();
		}
	}
	
	/**
	 * Hàm tạo mảng dạng key=> name của thuộc tính phòng KS
	 *
	 * @return mảng thuộc tính
	 */
    public static function getRoomFiltersForForm()
    {
    	static $filters;
    	
    	if(!isset($filters))
    	{
	    	self::getRoomFilters();
	    	
	        if(CGlobal::$room_filters)
	        {
	        	$filters = array();
	        	
	            foreach(CGlobal::$room_filters as &$filter)
	            {
	            	$filters[$filter['id']] = $filter['name'];
	            }
	        }
        }
        
        return $filters;
    }

    public static function getHotelFilersGroup()
    {
        static $filters_hotel_group;
        
        if(!isset($filters_hotel_group))
        {
        	self::getHotelFilters();
        	
        	$filters_hotel_group = CGlobal::$hotel_filter_groups;
        }
        
        return $filters_hotel_group;
    }

    public static function getHotelFilersByGroupId($gid)
    {
        if(!isset(CGlobal::$hotel_filters_bg[$gid]))
        {
            self::getHotelFilters();
        }

        if(isset(CGlobal::$hotel_filters_bg[$gid]))
        {
            return  CGlobal::$hotel_filters_bg[$gid];
        }
        
        return false;
    }
    
    public static function getSuggestion($keywords,$delcache = false)
    {
        if(ArrCache::is_not_cached('suggestion_arr',0,'',$delcache)){
            if(!$delcache){
                    $c = Db_Solr::autocomplete($keywords);
                    $r = array();
                    for($i=0;$i < sizeof($c); $i+=2)
                        $r[] = $c[$i];
                    CGlobal::$key_suggest[$keywords]	= $r;
                ArrCache::set(array('suggetion_result'=>CGlobal::$key_suggest));
            }else{
                foreach (CGlobal::$my_server as $server){//Tạo lại file js 
                    @fopen("http://$server/citygen.php","r");
                }
            }
        }
        else{
            $suggestion_arr	 			= ArrCache::get();
            CGlobal::$key_suggest  	= $suggestion_arr['suggetion_result'];
            }
            
	}
}
